package com.platform.service;

import com.platform.entity.UserCart;
import com.platform.entity.Product;
import com.platform.entity.OrderProduct;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.mapper.OrderProductMapper;
import com.platform.mapper.ProductMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderProductService extends ServiceImpl<OrderProductMapper, OrderProduct> {

    @Resource
    private OrderProductMapper orderproductMapper;
    @Resource
//    private ProductService productService;
    private ProductService productService;
    /**
     * 根据订单id查询订单关联的商品列表
     * @param orderId
     * @return
     */
    public List<UserCart> findByOrderId(Long orderId) {
        List<UserCart> carts = new ArrayList<>();
        List<OrderProduct> orderGoods = orderproductMapper.findGoodsByOrderId(orderId);
        for (OrderProduct orderGood : orderGoods) {
            Long goodsId = orderGood.getGoodsId();
            Product goods = productService.getById(goodsId);
            UserCart cart = new UserCart();
            cart.setGoods(goods);
            cart.setGoodsId(goodsId);
            cart.setCount(orderGood.getCount());
            carts.add(cart);
        }
        return carts;
    }
}
